<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-12
 * Time: 19:36
 */

namespace app\api\controller;
use app\common\model\ChatMsgPrivate as ChatMsgPrivateModel;
use app\common\model\User as UserModel;
use app\common\model\Grade as GradeModel;

use app\model\CardsModel;
use app\service\MsgService;
use app\service\UserService;

class Msg extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    //查询列表
    public function query_list(){
        $id=input('post.last_id');
        $token=input('post.token');
        //dump(md5('zkhb@2019'));die;
        if($token!=md5($id.'zkhb')){
            //$this->ajax(202,'身份校验失败');
        }
        $page=max(input('page'),1);
        $page_num=20;
        $result=MsgService::S()->queryListById($this->uid,$id,$page,$page_num);
        if(!$result) {
            $this->ajax(200,'success',[]);
        }
        $last_id=$result[0]['id'];
        foreach ($result as $k=>$v){
            $result[$k]['content']=json_decode($v['content'],true);
            $last_id=$v['id'];
        }
        $backData['list']=$result;
        $backData['last_id']=$last_id;
        $this->ajax(200,'success',$backData);
    }


    //查询聊天室未知用户信息
    public function user_list(){
        $uids=input('post.uids');
        $token=input('post.token');
        //dump(md5($uids.'zkhb'));die;
        if($token!=md5($uids.'zkhb')){
            //$this->ajax(202,'身份校验失败');
        }
        if(empty($uids)){
            $this->ajax(202,'用户ID不能为空');
        }
        $where['id']=array('in',$uids);
        $filed='id as uid,nickname,realname,headimgurl,grade_id,tag,uniid';
        $user=UserService::S()->selectUserInfo($where,$filed);
        foreach($user as $k=>$v) {
            $user[$k]['tag'] = $v['tag']?$v['tag']:($v['grade_id']==1?'':GradeModel::id2name($v['grade_id']));
            $cards = CardsModel::get(['user_id'=>$v['uid']]);
            $user[$k]['card_id'] = $cards?$cards['id']:'';
        }
        $this->ajax(200,'success',$user);
    }

    /* 密训消息列表 */
    public function privateList(){
        $list = ChatMsgPrivateModel::getAll(1,'ord asc,id asc');
        $referInfo = UserModel::getTopRefer($this->uid);
        if(!$referInfo) {
            $userInfo = [
                'nickname' => config('shop_name'),
                'headimgurl' => tomedia(config('share_picture'))
            ];
        }else{
            $userInfo = [
                'nickname' => $referInfo['nickname'],
                'headimgurl' => $referInfo['headimgurl']
            ];
        }

        if(empty($list)){
            //http://tjr.xixi05.cn/index.php/Api/Msg/privateList?mall_id=1
            $list=array();
            $this->ajax(200,'success',$list);
        }

        foreach($list as $k=>$v) {
            if(in_array($v['type'],[
                'img','voice','video'
            ])) {
                $list[$k]['content'] = tomedia($v['content']);
            }
            if($v['type']=='voice') {
                $info = file_get_contents($list[$k]['content']."?avinfo");
                $info = json_decode($info,true);
                $list[$k]['time'] = (int)$info['format']['duration'];
            }
            $list[$k]['userInfo'] = $userInfo;
        }
        $this->ajax(200,'success',$list);
    }

   
}